C++
Nils Wiki>Formal Sciences>Computer Sciences>Software Systems>Programming languages>C++ ---- Libraries #include The standard library of C++ #include Required to process in- and output from user #include Required to process strings #include #include Datatypes int variable Can store an integers float variable Can store decimal numbers double variable Can store a wider range of decimal numbers char variable Can store a single character void variable Valueless special purpose type string variable Creates a string which can store a series of letters (Requires #include) getline (cin, mystr); Let the user store a sentence in mystr Arithmetic Operators =''' Sets a value to a variable '''+-*/ As mathematical operators Equal to != Not equal to >''' Grater than '''< Less than >= Grater than, or equal to <= Less than, or equal to %''' Remainder Division '''+= a += b means a = a + b '-=' a -= b means a = a - b *= a *= b means a = a * b /= a /= b means a = a / b c++ c+=1 means c=c+1 Logical Operators &''' '''AND |''' '''OR Inclusive OR ^''' '''XOR Exclusive OR ~''' '''NOT Unary complement (bit inversion) && This operation results true if both its two operands are true, and false otherwise || This operation results true if either one of its two operands, or both, are true, but it results false only if both operands are false. !' Produce the opposite boolean value. !(5 5) evaluates to false,because the expression at its right (5 5) is true. '? condition ? result1 : result2 Returns one value if condition is true, another one if it is false Escape Codes \n newline \r carriage return \t tab \v vertical tab \b backspace \f form feed (page feed) \a alert (beep) ' \' ' single quote (') \" double quote (") \? question mark (?) \\ backslash (\) Input&Output cout << It prints on the screen, whatever you tell it write cin >> a Lets the user write a value into variable "a", and does not continiue until "Enter" is pressed cout << a Shows the user, the value of variable "a" Control Loops while( condition )' ''statement; '''do while statement; '''while (' condition) for ( initialization; condition; increase ) statement; Jump statements: break for (n=10; n>0; n--) { cout << n << ", "; if (n 3) { cout << "countdown aborted!"; break; } } return 0; continue continiue for (int n=10; n>0; n--) { if (n 5) continue; cout << n << ", "; }cout << "FIRE!\n"; return 0; goto int n=10;loop:cout << n << ", "; n--;if (n>0) goto loop;cout << "FIRE!\n"; return 0; exit void exit (int exitcode); switch switch (expression){case constant1:group of statements 1;break;case constant2:group of statements 2;break;default:default group of statements} Conditional Execution: if (total>O) printf("OK"); if else if (condition) statement1;else statement2; if else if else ; used at the end of a statement , Comma is used to separate two or more expressions that are included where only one expression is expected. a = (b=3, b+2); << SHL Shift Left >> SHR Shift Right change case int i; float f = 3.14; i = (int) f; a = sizeof (char); Constants: 75 // decimal 0113 // octal 0x4b // hexadecimal 75 // int 75u // unsigned int 75l // long 75ul // unsigned long 'p' "Hello world" L"This is a wide character string" #define PI 3.14159265 const int pathwidth = 100; Structure and Nesting: Functions: A function is simply a chunk of C code (statements) that you have grouped together and given a name. Use that "chunk" of code repeatedly simply by writing its name demo(){printf("Hello"); total = total + 1;} Once you have defined your function you can use it within a program: main(){demo();} you can declare variables within it You can define special variables called parameters which are used to carry data values into a function sum( int a, int b){int result;result=a + b;} sum(l,2);is a call to the sum function with a set to 1 and b set to 2 A function can return a value via its name - it's as if the name was a variable and had a value. return result; int sum(int a, int b){int result;result = a + b;return result;} declare the function separately at the start of the program. For example: int sum();main(){etc... main(){} The main Program-Code,sort of. demo(){} Can be used in the main() - function, as a seperat block of code, that erases itself at the end of the block. Arrays: main() { int al,a2,a3,a4,a5; scanf("%d %d %d %d %d",&a1,&a2,&a3,&a4,&a5); printf("%d %d %d %d %d'',a5,a4,a3,a2,a1); } Shorts down the amount of int or char you have to put into the code. main() { char a5; int i; for(i=0;i<5;++i) scanf("%c",&ai); for(i=4;i>=0;--i) printf("%c",ai); } Pointers: int *a Creates a pointer. A pointer can store a certain variables adress, in other words, find a variables value. & is the reference operator and can be read as "address of" * is the dereference operator and can be read as "value pointed by"